module (...,package.seeall)
	local storyboard = require( "storyboard" )
	local scene = storyboard.newScene()
	local ui = require ("ui")
	require "sprite"
	local wEasing = require "wEasing"
	local gui = require "Stage11.Stage11HomeGui".new()
function scene:createScene(event)
	local mainHomeGroup = display.newGroup()
	local arrayPosition = {}--mang cac vi tri
	local arrayFloat = {}
	local timeReady = 3
	local totalTime = 20000
	local delay = 100
	local loop = 0
	local instance, instance1, instance2, instance3
	local score = 0
	local bg = display.newImageRect(mainHomeGroup,gui.bg.fn, gui.bg.w, gui.bg.h)
		bg.x = gui.bg.x
		bg.y = gui.bg.y
	a = 0
	for i = 1,7 do
		arrayPosition[i] = display.newImageRect(mainHomeGroup,gui.empty.fn, gui.empty.w, gui.empty.h)
		arrayPosition[i].x = gui.empty.x+a
		arrayPosition[i].y = gui.empty.y
		--arrayPosition[i]:setFillColor(0,0,255)
		a = a +45
	end
	local txtTimeReady = display.newText(mainHomeGroup, timeReady,gui.txtTimeReady.x , gui.txtTimeReady.y, gui.txtTimeReady.font, gui.txtTimeReady.size+20)
		txtTimeReady:setTextColor(unpack(gui.txtTimeReady.color))
		txtTimeReady.alpha = 1
	local txtTimePlay = display.newText(mainHomeGroup, totalTime/1000,gui.txtTimePlay.x -20, gui.txtTimePlay.y, gui.txtTimePlay.font, gui.txtTimeReady.size)
		txtTimePlay:setTextColor(unpack(gui.txtTimePlay.color))
		txtTimePlay.alpha = 0
	local txtScore = display.newText(mainHomeGroup, score/2,160, gui.txtTimePlay.y-70, gui.txtTimePlay.font, gui.txtTimeReady.size)
		txtScore:setTextColor(unpack(gui.txtTimePlay.color))
		txtScore.alpha = 0
	local gameOver = display.newText(mainHomeGroup,"GAME OVER", gui.txtTimeReady.x, gui.txtTimeReady.y+30, gui.txtTimeReady.font, gui.txtTimeReady.size)
		gameOver.x = display.contentCenterX
		gameOver:setTextColor(255,0,0)
		gameOver.alpha = 0
	local person = display.newImageRect(mainHomeGroup,gui.person.fn, gui.person.w, gui.person.h)
		person.x = arrayPosition[2].x
		person.y = arrayPosition[2].y-70
		person.alpha = 0
	local btnBlueFloat = display.newImageRect(mainHomeGroup,gui.btnBlueFloat.fn, gui.btnBlueFloat.w, gui.btnBlueFloat.h)
		btnBlueFloat.x = gui.btnBlueFloat.x
		btnBlueFloat.y = gui.btnBlueFloat.y
		btnBlueFloat.alpha = 0
	local btnRedFloat = display.newImageRect(mainHomeGroup,gui.btnRedFloat.fn, gui.btnRedFloat.w, gui.btnRedFloat.h)
		btnRedFloat.x = gui.btnRedFloat.x
		btnRedFloat.y = gui.btnRedFloat.y
		btnRedFloat.alpha = 0
	function animationJump()
		mySheet = graphics.newImageSheet("Images/Stage11/s-nhay-2.png",{width = 50,height =150 , numFrames = 5,sheetContentHeight = 150, sheetContentWidth = 250})
		instance = display.newSprite( mySheet, { name="jump", start = 1, count = 5, time=600, loopCount = 1} )
		instance.x = person.x
		instance.y = person.y
		person.alpha = 0
		instance:play()
	end
	function animationDownWater()
		mySheet1 = graphics.newImageSheet("Images/Stage11/jumper2.png",{width = 50, height = 150, numFrames = 13,sheetContentWidth =650 , sheetContentHeight =150 })
		instance1 = display.newSprite(mySheet1,{name = "down water",start = 1, count = 13, time = 600, loopCount = 1})
		instance1.x = person.x
		instance1.y = person.y
		person.alpha = 0
		instance1:play()
	end
	function animationBlueFloat()
		mySheet2= graphics.newImageSheet("Images/Stage11/spriteBlueFloat.png",{width = 40, height = 40*38/77, numFrames = 3,sheetContentWidth = 120, sheetContentHeight =40*38/77 })
		instance2 = display.newSprite(mySheet2,{name = "down water",start = 1, count = 3, time = 600, loopCount = 1})
		instance2.x = arrayPosition[2].x
		instance2.y = arrayPosition[2].y
		instance2:play()
	end
	function animationRedFloat()
		mySheet3 = graphics.newImageSheet("Images/Stage11/spriteRedFloat.png",{width = 40, height = 40*38/77, numFrames = 3,sheetContentWidth = 120, sheetContentHeight =40*38/77 })
		instance3 = display.newSprite(mySheet3,{name = "down water",start = 1, count = 3, time = 600, loopCount = 1})
		instance3.x = arrayPosition[2].x
		instance3.y = arrayPosition[2].y
		instance3:play()
	end
		
	function countdownTime (event)
		txtTimePlay.alpha = 1
		 txtTimePlay.text = (math.round((totalTime - tonumber((system.getTimer() - timeSystem)))/100))/10 ..""
		 if( totalTime <(system.getTimer() - timeSystem)) then
			  txtTimePlay.text = "0"
			  gameOver.alpha = 1
			  timer.cancel(timer_countdown)
		end
	end	
	function countdownReady ()
		timeReady = timeReady - 1
			txtTimeReady.text = timeReady
			if txtTimeReady ~= nil and statusBoard== "start" then
				txtTimeReady.alpha = 1
			end
		if timeReady <0 then
			txtTimeReady.alpha = 1
			txtTimeReady.text = "GO"
		end
		if timeReady <-1 then
			txtTimeReady.alpha = 0
			viewFloat()
			timer.cancel(timer_countdownReady)
			
			txtTimePlay.alpha = 1
			timer_countdown = timer.performWithDelay(delay, countdownTime, loop)
			timeSystem = system.getTimer()
		end
	end
	timer_countdownReady = timer.performWithDelay(1000,countdownReady,0)
	function viewFloat()
		arrayFloat[1] = display.newImageRect(mainHomeGroup,gui.empty.fn, gui.empty.w, gui.empty.h)
		arrayFloat[1].x = arrayPosition[1].x
		arrayFloat[1].y = arrayPosition[1].y
		arrayFloat[1].empty = true
		for i = 2,5 do
			arrayFloat[i] = display.newImageRect(mainHomeGroup,gui.blueFloat.fn, gui.blueFloat.w, gui.blueFloat.h)
			arrayFloat[i].x = arrayPosition[i].x
			arrayFloat[i].y = arrayPosition[i].y
			arrayFloat[i].empty = false
		end
			arrayFloat[6] = display.newImageRect(mainHomeGroup,gui.empty.fn, gui.empty.w, gui.empty.h)
			arrayFloat[6].x = arrayPosition[6].x
			arrayFloat[6].y = arrayPosition[6].y
			arrayFloat[6].empty = true
			arrayFloat[7] = display.newImageRect(mainHomeGroup,gui.redFloat.fn, gui.redFloat.w, gui.redFloat.h)
			arrayFloat[7].x = arrayPosition[7].x
			arrayFloat[7].y = arrayPosition[7].y
			arrayFloat[7].empty = false
		
		person.alpha = 1
		btnBlueFloat.alpha = 1
		btnRedFloat.alpha = 1
		txtScore.alpha = 1
	end
	
	function randomPosition1()
		a = #arrayFloat
		if instance ~= nil then
			display.remove(instance)
		end
		if instance1 ~= nil then
			display.remove(instance1)
		end
		if instance2 ~= nil then
			display.remove(instance2)
		end
		if instance3 ~= nil then
			display.remove(instance3)
		end
		
		if arrayFloat[a-4].empty == false then---thuc hien nhay 1 phao
			animationJump()
			score = math.round((a-6)/2*10)/10
			txtScore.text = score
			timefloat = timer.performWithDelay(400,function() animationBlueFloat() end, 1)
		else	
			---roi xuong nuoc
			animationDownWater()
			gameOver.alpha = 1
			print ("roi xuong nuoc")
			btnBlueFloat.alpha = 0
			btnRedFloat.alpha = 0
			timer.cancel(timer_countdown)
		end
		aa = timer.performWithDelay(100,function()
												if arrayFloat[a].empty == true then
													arrayFloat[a+1] = display.newImageRect(mainHomeGroup,gui.redFloat.fn, gui.redFloat.w, gui.redFloat.h)
													arrayFloat[a+1].x = arrayPosition[7].x 
													arrayFloat[a+1].y = arrayPosition[7].y
													arrayFloat[a+1].empty = false
												else
													random1 = math.random(1,2)
													print(a)
													if random1 == 1 then --------phao xanh
														arrayFloat[a+1] = display.newImageRect(mainHomeGroup,gui.blueFloat.fn, gui.blueFloat.w, gui.blueFloat.h)
														arrayFloat[a+1].x = arrayPosition[7].x 
														arrayFloat[a+1].y = arrayPosition[7].y
														arrayFloat[a+1].empty = false
													else ---trống-->sinh thêm ô kế tiếp là phao đỏ
														arrayFloat[a+1] = display.newImageRect(mainHomeGroup,gui.empty.fn, gui.empty.w, gui.empty.h)
														arrayFloat[a+1].x = arrayPosition[7].x 
														arrayFloat[a+1].y = arrayPosition[7].y
														arrayFloat[a+1].empty = true
													end
												end
												j = 1
												for i = a-5,a do
													arrayFloat[i].x = arrayPosition[j].x
													arrayFloat[i].y = arrayPosition[j].y
													j = j+1
												end
											end,1)
			display.remove(arrayFloat[a-6])
	end
	function randomPosition2()
		a = #arrayFloat
		if instance ~= nil then
			display.remove(instance)
		end
		if instance1 ~= nil then
			display.remove(instance1)
		end
		if instance2 ~= nil then
			display.remove(instance2)
		end
		if instance3 ~= nil then
			display.remove(instance3)
		end	
		
		if arrayFloat[a-3].empty == false then---thuc hien nhay 2 phao
			animationJump()
			score = math.round((a-5)/2*10)/10
			txtScore.text = score
			if arrayFloat[a-4].empty == true then
				timefloat = timer.performWithDelay(400,function() animationRedFloat() end, 1)
			else 
				timefloat = timer.performWithDelay(400,function() animationBlueFloat() end, 1)
			end
		else	
			---roi xuong nuoc
			animationDownWater()
			gameOver.alpha = 1
			timer.cancel(timer_countdown)
			print ("roi xuong nuoc")
			btnBlueFloat.alpha = 0
			btnRedFloat.alpha = 0
		end
		bb = timer.performWithDelay(100, function()
												if arrayFloat[a].empty == true then
													arrayFloat[a+1] = display.newImageRect(mainHomeGroup,gui.redFloat.fn, gui.redFloat.w, gui.redFloat.h)
													arrayFloat[a+1].x = arrayPosition[6].x 
													arrayFloat[a+1].y = arrayPosition[6].y
													arrayFloat[a+1].empty = false
													random2 = math.random(1,2)
													if random2 == 1 then
														arrayFloat[a+2] = display.newImageRect(mainHomeGroup,gui.blueFloat.fn, gui.blueFloat.w, gui.blueFloat.h)
														arrayFloat[a+2].x = arrayPosition[7].x 
														arrayFloat[a+2].y = arrayPosition[7].y
														arrayFloat[a+2].empty = false
													else
														arrayFloat[a+2] = display.newImageRect(mainHomeGroup,gui.empty.fn, gui.empty.w, gui.empty.h)
														arrayFloat[a+2].x = arrayPosition[7].x 
														arrayFloat[a+2].y = arrayPosition[7].y
														arrayFloat[a+2].empty = true
													end
												else
													random1 = math.random(1,2)
													if random1 == 1 then
														arrayFloat[a+1] = display.newImageRect(mainHomeGroup,gui.blueFloat.fn, gui.blueFloat.w, gui.blueFloat.h)
														arrayFloat[a+1].x = arrayPosition[6].x 
														arrayFloat[a+1].y = arrayPosition[6].y
														arrayFloat[a+1].empty = false
														random2 = math.random(1,2)
														if random2 == 1 then
															arrayFloat[a+2] = display.newImageRect(mainHomeGroup,gui.blueFloat.fn, gui.blueFloat.w, gui.blueFloat.h)
															arrayFloat[a+2].x = arrayPosition[7].x 
															arrayFloat[a+2].y = arrayPosition[7].y
															arrayFloat[a+2].empty = false
														else
															arrayFloat[a+2] = display.newImageRect(mainHomeGroup,gui.empty.fn, gui.empty.w, gui.empty.h)
															arrayFloat[a+2].x = arrayPosition[7].x 
															arrayFloat[a+2].y = arrayPosition[7].y
															arrayFloat[a+2].empty = true
														end
													else 
														arrayFloat[a+1] = display.newImageRect(mainHomeGroup,gui.empty.fn, gui.empty.w, gui.empty.h)
														arrayFloat[a+1].x = arrayPosition[6].x 
														arrayFloat[a+1].y = arrayPosition[6].y
														arrayFloat[a+1].empty = true
														arrayFloat[a+2] = display.newImageRect(mainHomeGroup,gui.redFloat.fn, gui.redFloat.w, gui.redFloat.h)
														arrayFloat[a+2].x = arrayPosition[7].x 
														arrayFloat[a+2].y = arrayPosition[7].y
														arrayFloat[a+2].empty = false
													end
												end
												j = 1
												for i = a-4,a do
													arrayFloat[i].x = arrayPosition[j].x
													arrayFloat[i].y = arrayPosition[j].y
													j = j+1
												end
											end,1)
			display.remove(arrayFloat[a-5])
			display.remove(arrayFloat[a-6])
	end
	btnBlueFloat:addEventListener("tap", randomPosition1)
	btnRedFloat:addEventListener("tap",randomPosition2)
	return mainHomeGroup
end
-- Called immediately after scene has moved onscreen:
function scene:enterScene( event )
	--local group = self.view

	-- audio.play( sfx.themeMusic, { onComplete = function() 
                                      -- audio.dispose( sfx.themeMusic ) 
                                      -- end } )
	-----------------------------------------------------------------------------
		
	--	INSERT code here (e.g. start timers, load audio, start listeners, etc.)
	
	-----------------------------------------------------------------------------
	
end


-- Called when scene is about to move offscreen:
function scene:exitScene( event )
	local group = self.view
	
	-----------------------------------------------------------------------------
	
	--	INSERT code here (e.g. stop timers, remove listeners, unload sounds, etc.)
	
	-----------------------------------------------------------------------------
	
end


-- Called prior to the removal of scene's "view" (display group)
function scene:destroyScene( event )
	local group = self.view
	
	-----------------------------------------------------------------------------
	
	--	INSERT code here (e.g. remove listeners, widgets, save state, etc.)
	
	-----------------------------------------------------------------------------
	
end

---------------------------------------------------------------------------------
-- END OF YOUR IMPLEMENTATION
---------------------------------------------------------------------------------

-- "createScene" event is dispatched if scene's view does not exist
scene:addEventListener( "createScene", scene )

-- "enterScene" event is dispatched whenever scene transition has finished
scene:addEventListener( "enterScene", scene )

-- "exitScene" event is dispatched before next scene's transition begins
scene:addEventListener( "exitScene", scene )

-- "destroyScene" event is dispatched before view is unloaded, which can be
-- automatically unloaded in low memory situations, or explicitly via a call to
-- storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( "destroyScene", scene )

---------------------------------------------------------------------------------

return scene
